<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
use Home\Model\ContentCollectModel;
class CountController extends Controller {

    	
     //当前任务采集的有效数据条数，在contentCollect中做统计,task_type:1问答，2新闻
    public function taskValueDataCount(){
    	if(session('name')){
    		$dataCount = M('ContentCollect');
    		$task = M('Task');
    		$taskMap['task_type'] = I('param.task_type',1);
    		$taskArr = $task->field('id')->where($taskMap)->select();
    		$countMap = array();
    		if($taskArr){
    			foreach ($taskArr as $task){
    				$countMap[]['task_id'] = $task['id'];
    			}
    			$countMap['_logic'] = 'OR';
    			echo json_encode(array('count'=>$dataCount->where($countMap)->count(),'code'=>1));
    		}else{
    			echo json_encode(array('count'=>0,'code'=>1));
    		}
    		
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    
    //当前任务采集问答条数统计/采集线程总数/采集成功/失败数
    public function taskDataCount(){
    	if(session('name')){
    		$task = M('Task');
	    	$taskMap['task_status'] = 1;
	    	$taskMap['task_type'] = I('param.task_type',1);
	    	$taskArr = $task->field(array('SUM(count_thread) as count_thread','SUM(count_success) as count_success','SUM(count_failed) as count_failed'))->where($taskMap)->select();
	    	echo json_encode(array('count'=>$taskArr[0],'code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    	
    }
    
    //当天采集问答数
    public function nowAnswerCount(){
    	if(session('name')){
    		$dataCount = M('ContentCollect');
    		$type = I('param.task_type',1);
    		$sql = "select count(id) as count from spider_content_collect where ";
    		if($type !=0){
    			$sql .= "task_type=$type";
    		}else{
    			$sql .= "alert=1";
    		}
    		$sql .= " AND to_days(FROM_UNIXTIME( time, '%Y-%m-%d' ))=to_days(now())";
    		$countArr = $dataCount->query($sql);
    		echo json_encode(array('count'=>$countArr[0]['count'],'code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    
    //所有采集问答数
    public function allAnswerCount(){
    	if(session('name')){
    		$dataCount = M('ContentCollect');
    		$type = I('param.task_type',1);
    		$sql = "select count(id) as count from spider_content_collect where ";
    		if($type !=0){
    			$sql .= "task_type=$type";
    		}else{
    			$sql .= "alert=1";
    		}
    		$countArr = $dataCount->query($sql);
    		echo json_encode(array('count'=>$countArr[0]['count'],'code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    
    //得到当前任务数,task_type:1问答，2新闻
    public function taskCount(){
    	if(session('name')){
    		$dataCount = M('ContentCollect');
    		$type = I('param.task_type',1);
    		$sql = "select count(id) as count from spider_task where task_type=$type and task_status=1";
    		$countArr = $dataCount->query($sql);
    		echo json_encode(array('count'=>$countArr[0]['count'],'code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    
    //得到默认的删除天数
    public function getDeleteDay(){
    	if(session('name')){
    		$sdTable = M('Dict');
    		$where['dict_name'] = 'delete_day';
    		$sd = $sdTable->field('dict_value')->where($where)->select();
    		echo json_encode(array('result'=>intval($sd[0]['dict_value']),'code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
   
}